/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasyleagueFacade;
import beans.ejb.FantasyrosterperweekFacade;
import beans.ejb.FantasyteamFacade;
import beans.ejb.FantasyuserFacade;
import beans.ejb.NflplayerFacade;
import entities.Fantasyleague;
import entities.Fantasyrosterperweek;
import entities.Fantasyteam;
import entities.Fantasyuser;
import entities.Nflplayer;
import entities.Week;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author nujwoo
 */
@ManagedBean
@SessionScoped
public class RosterBean implements Serializable {
   
    private Fantasyteam myTeam;
    private List<Fantasyteam> teams;
    private Week week;
    private List<Fantasyrosterperweek> rosterList;
    private Fantasyuser myId;
    private Fantasyleague myleague;
    private ArrayList<ArrayList<String>> data;
    private Map<String,String> params;
    private Nflplayer selectedPlayer;
    private String message;
    private List<Nflplayer> playerList;
    private int counter = 0;
    
    private String qb1;
    private String wo1;
    private String rb1;
    private String rb2;
    private String wr1;
    private String wr2;
    private String te1;
    private String kk1;
    private String def;
    
    @EJB
    private FantasyuserFacade userFacade;
    @EJB
    private FantasyteamFacade teamFacade;
    @EJB
    private FantasyrosterperweekFacade weekFacade;
    @EJB
    private NflplayerFacade playerFacade;
  
    
    public RosterBean() 
    {
        
        
    }
    
   
        
    

    public Fantasyteam getMyTeam() 
    {
        
        
        return myTeam;
    }

    public void setMyTeam(Fantasyteam myTeam) {
        this.myTeam = myTeam;
    }

    public Week getWeek() {
        return week;
    }

    public void setWeek(Week week) {
        this.week = week;
    }
    
    public ArrayList<ArrayList<String>> getDropData()
    {
        if(myId == null)
        {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean)request.getSession().getAttribute("accountBean");
        
        String userId = b.getId().toString();
        
        this.myId = userFacade.getUserById(userId);
        this.myleague = b.getCurrentLeague();
        
        this.myTeam = teamFacade.getTeamId(myleague, myId);
        this.week = b.getCurrentWeek();
        
        rosterList = weekFacade.findByTeamByWeek(myTeam, week);
        System.err.println("1");
        data = new ArrayList<ArrayList<String>>();
        
        
        int qb = 1;
        int rb = 1;
        int wr = 1;
        int te = 1;
        int kk = 1;
        int def = 1;
        
        
        int size = rosterList.size();
        for(int i = 0 ; i < size ; i++)
        {
            ArrayList<String> ar = new ArrayList<String>();
            
            Fantasyrosterperweek roster = rosterList.get(i);
            
            Nflplayer p = roster.getPlayerId();
            
            String pos = p.getPosition();
            
            //0
            if(pos.equals("QB"))
            {
                String s = String.valueOf(qb);
                ar.add(pos+s);
                qb++;
            }
            else if(pos.equals("RB"))
            {
                String s = String.valueOf(rb);
                ar.add(pos+s);
                rb++;
            }
            else if(pos.equals("WR"))
            {
                String s = String.valueOf(wr);
                ar.add(pos+s);
                wr++;
            }
            else if(pos.equals("TE"))
            {
                String s = String.valueOf(te);
                ar.add(pos+s);
                te++;
            }
            else if(pos.equals("K"))
            {
                String s = String.valueOf(kk);
                ar.add(pos+"K"+s);
                kk++;
            }
            else if(pos.equals("DE"))
            {
                String s = String.valueOf(def);
                ar.add(pos+s);
                def++;
            }
            
            
            
                
            //1 - name
            ar.add(p.getFirstName()+" "+p.getLastName());
            //2 - position
            ar.add(pos);
            //3 - player id
            ar.add(p.getId().toString());
            
            String fPos = roster.getFantasyPositionInRoster();
            //4 - FantasyPosition BN or NotBN
            if(fPos.equals("BN"))
            {
                ar.add("BN");
            }
            else
            {
                ar.add("NotBN");
            }
            
            //5 - FantasyPosition
            ar.add(fPos);
            
            
            
            data.add(ar);
            
        }
               
        }
        return data;
    }
   

    public ArrayList<ArrayList<String>> getData() 
    {
        
        
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean)request.getSession().getAttribute("accountBean");
        
        String userId = b.getId().toString();
        
        this.myId = userFacade.getUserById(userId);
        this.myleague = b.getCurrentLeague();
        
        this.myTeam = teamFacade.getTeamId(myleague, myId);
        this.week = b.getCurrentWeek();
        
        rosterList = weekFacade.findByTeamByWeek(myTeam, week);
        
        data = new ArrayList<ArrayList<String>>();
        
        
        int qb = 1;
        int rb = 1;
        int wr = 1;
        int te = 1;
        int kk = 1;
        int def = 1;
        
        
        int size = rosterList.size();
        for(int i = 0 ; i < size ; i++)
        {
            ArrayList<String> ar = new ArrayList<String>();
            
            Fantasyrosterperweek roster = rosterList.get(i);
            
            Nflplayer p = roster.getPlayerId();
            
            String pos = p.getPosition();
            
            //0
            if(pos.equals("QB"))
            {
                String s = String.valueOf(qb);
                ar.add(pos+s);
                qb++;
            }
            else if(pos.equals("RB"))
            {
                String s = String.valueOf(rb);
                ar.add(pos+s);
                rb++;
            }
            else if(pos.equals("WR"))
            {
                String s = String.valueOf(wr);
                ar.add(pos+s);
                wr++;
            }
            else if(pos.equals("TE"))
            {
                String s = String.valueOf(te);
                ar.add(pos+s);
                te++;
            }
            else if(pos.equals("K"))
            {
                String s = String.valueOf(kk);
                ar.add(pos+"K"+s);
                kk++;
            }
            else if(pos.equals("DE"))
            {
                String s = String.valueOf(def);
                ar.add(pos+s);
                def++;
            }
            
            
            
                
            //1 - name
            ar.add(p.getFirstName()+" "+p.getLastName());
            //2 - position
            ar.add(pos);
            //3 - player id
            ar.add(p.getId().toString());
            
            String fPos = roster.getFantasyPositionInRoster();
            //4 - FantasyPosition BN or NotBN
            if(fPos.equals("BN"))
            {
                ar.add("BN");
            }
            else
            {
                ar.add("NotBN");
            }
            
            //5 - FantasyPosition
            ar.add(fPos);
            
            
            
            data.add(ar);
            
        }
               
        
        return data;
    }

    public void action() 
    {
        //FacesContext context = FacesContext.getCurrentInstance();
        //params = context.getExternalContext().getRequestParameterMap();
        
       for(int i = 0; i < rosterList.size(); i++)
       {
           rosterList.get(i).setFantasyPositionInRoster("BN");
           
           String playerId = rosterList.get(i).getPlayerId().getId().toString();
           
           if(playerId.equals(qb1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_QB1");
           }
           else if(playerId.equals(wo1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_Wo1");
           }
           else if(playerId.equals(rb1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_RB1");
           }
           else if(playerId.equals(rb2))
           {
               rosterList.get(i).setFantasyPositionInRoster("_RB2");
           }
           else if(playerId.equals(wr1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_WR1");
           }
           else if(playerId.equals(wr2))
           {
               rosterList.get(i).setFantasyPositionInRoster("_WR2");
           }
           else if(playerId.equals(te1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_TE1");
           }
           else if(playerId.equals(kk1))
           {
               rosterList.get(i).setFantasyPositionInRoster("_KK1");
           }
           else if(playerId.equals(def))
           {
               rosterList.get(i).setFantasyPositionInRoster("_DEF");
           }
       }
       
        weekFacade.updateRoster(rosterList);
        
        
    }

    
    
    public void setData(ArrayList<ArrayList<String>> data) {
        this.data = data;
    }
    
    

    public List<Fantasyrosterperweek> getRosterList() {
        
 
        return rosterList;
        
    }

    public void setRosterList(List<Fantasyrosterperweek> rosterList) {
        this.rosterList = rosterList;
    }

    public FantasyuserFacade getUserFacade() {
        return userFacade;
    }

    public void setUserFacade(FantasyuserFacade userFacade) {
        this.userFacade = userFacade;
    }

    public FantasyteamFacade getTeamFacade() {
        return teamFacade;
    }

    public void setTeamFacade(FantasyteamFacade teamFacade) {
        this.teamFacade = teamFacade;
    }

    public FantasyrosterperweekFacade getWeekFacade() {
        return weekFacade;
    }

    public void setWeekFacade(FantasyrosterperweekFacade weekFacade) {
        this.weekFacade = weekFacade;
    }

    public Fantasyuser getMyId() {
        return myId;
    }

    public void setMyId(Fantasyuser myId) {
        this.myId = myId;
    }

    public Fantasyleague getMyleague() {
        return myleague;
    }

    public void setMyleague(Fantasyleague myleague) {
        this.myleague = myleague;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public String getQb1() {
        return qb1;
    }

    public void setQb1(String qb1) {
        this.qb1 = qb1;
    }

    public String getWo1() {
        return wo1;
    }

    public void setWo1(String wo1) {
        this.wo1 = wo1;
    }

    public String getRb1() {
        return rb1;
    }

    public void setRb1(String rb1) {
        this.rb1 = rb1;
    }

    public String getRb2() {
        return rb2;
    }

    public void setRb2(String rb2) {
        this.rb2 = rb2;
    }

    public String getWr1() {
        return wr1;
    }

    public void setWr1(String wr1) {
        this.wr1 = wr1;
    }

    public String getWr2() {
        return wr2;
    }

    public void setWr2(String wr2) {
        this.wr2 = wr2;
    }

    public String getTe1() {
        return te1;
    }

    public void setTe1(String te1) {
        this.te1 = te1;
    }

    public String getKk1() {
        return kk1;
    }

    public void setKk1(String kk1) {
        this.kk1 = kk1;
    }

    public String getDef() {
        return def;
    }

    public void setDef(String def) {
        this.def = def;
    }
    
    public boolean isDisableBTN() {
        int remainDraft = 15;
        
        rosterList = weekFacade.findByTeamByWeek(myTeam, week);
        int size = rosterList.size();
        
        remainDraft = remainDraft - size;
        
        if(remainDraft <= 0)
        {
            message = "Roster is Full - ";
            return false;
        }
        
        return true;
    }
    
    

    public Nflplayer getSelectedPlayer() 
    {
        return selectedPlayer;
    }

    public void setSelectedPlayer(Nflplayer p) 
    {
        this.selectedPlayer = p;
    }
    
    public List<Nflplayer> getPlayerList()
    {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean)request.getSession().getAttribute("accountBean");
        
        
        
        //List
        playerList = playerFacade.findAll();
        
        this.myleague = b.getCurrentLeague();
        this.week = b.getCurrentWeek();
        
        this.teams = teamFacade.getAllTeams(myleague);
        
        for(int i=0; i < teams.size() ; i++)
        {
            Fantasyteam t = teams.get(i);
            rosterList = weekFacade.findByTeamByWeek(t, week);
            for(int j=0 ; j < rosterList.size() ; j++)
            {
                  playerList.remove(rosterList.get(j).getPlayerId());
            }
        }
        
        
        
        return playerList;
    }
    
    public void setPlayerList()
    {
        
    }
    
    public void addPlayer()
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Map <String, String> m = context.getExternalContext().getRequestParameterMap();
        
        String action = m.get("action");
        
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean)request.getSession().getAttribute("accountBean");
        
        String userId = b.getId().toString();
        
        
        if(isDisableBTN())
        {
            
            selectedPlayer = playerFacade.getPlayerById(action);
            this.myId = userFacade.getUserById(userId);
            this.myleague = b.getCurrentLeague();
            this.myTeam = teamFacade.getTeamId(myleague, myId);
            this.week = b.getCurrentWeek();
            
            
            
            Fantasyrosterperweek frpw = new Fantasyrosterperweek();
            frpw.setPlayerId(selectedPlayer);
            frpw.setTeamId(myTeam);
            frpw.setFantasyPositionInRoster("BN");
            frpw.setWeekId(week);
            
            weekFacade.create(frpw);
            message = "Player " + selectedPlayer.getFirstName() + 
                        " "+selectedPlayer.getLastName()+" is added!";
        }
        else
        {
            message += "Cannot Be Added!";
        }
        
        
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    private String del;

    public List<Fantasyteam> getTeams() {
        return teams;
    }

    public void setTeams(List<Fantasyteam> teams) {
        this.teams = teams;
    }

    public NflplayerFacade getPlayerFacade() {
        return playerFacade;
    }

    public void setPlayerFacade(NflplayerFacade playerFacade) {
        this.playerFacade = playerFacade;
    }

    public String getDel() {
        return del;
    }

    public void setDel(String del) {
        this.del = del;
        
    }
    
    
    public void delete()
    {
        String[] ar = del.split(",");
        
        
        FacesContext context = FacesContext.getCurrentInstance();
        Map <String, String> m = context.getExternalContext().getRequestParameterMap();
        
        String action = m.get("action");
        
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean)request.getSession().getAttribute("accountBean");
        
        String userId = b.getId().toString();
        
        message = "";
        for(int i = 0 ; i < ar.length ; i++)
        {
        

            selectedPlayer = playerFacade.getPlayerById(ar[i]);
            this.myId = userFacade.getUserById(userId);
            this.myleague = b.getCurrentLeague();
            this.myTeam = teamFacade.getTeamId(myleague, myId);
            this.week = b.getCurrentWeek();

            weekFacade.findByTeamIdByWeekIdByPlayerId(myTeam,week,selectedPlayer);

            message += "Deleted! ";

        
        }
        
        
        
        
        
        
        
        
        System.out.println("12");
    }
    
}
